package org.jeecg.modules.wms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.wms.entity.WmsPurchaseConfig;
import org.jeecg.modules.wms.service.IWmsPurchaseConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 采购配置管理
 * @Author: jeecg-boot
 * @Date: 2023-07-17
 * @Version: V1.0
 */
@Api(tags="采购配置管理")
@RestController
@RequestMapping("/wms/purchase/auto")
@Slf4j
public class WmsPurchaseConfigController extends JeecgController<WmsPurchaseConfig, IWmsPurchaseConfigService> {
    @Autowired
    private IWmsPurchaseConfigService wmsPurchaseConfigService;
    
    /**
     * 获取采购配置
     *
     * @return
     */
    @AutoLog(value = "采购配置-获取配置")
    @ApiOperation(value="采购配置-获取配置", notes="采购配置-获取配置")
    @GetMapping(value = "/config")
    public Result<WmsPurchaseConfig> getConfig() {
        WmsPurchaseConfig config = wmsPurchaseConfigService.getDefaultConfig();
        return Result.OK(config);
    }
    
    /**
     * 更新采购配置
     *
     * @param wmsPurchaseConfig
     * @return
     */
    @AutoLog(value = "采购配置-更新配置")
    @ApiOperation(value="采购配置-更新配置", notes="采购配置-更新配置")
    @PutMapping(value = "/config")
    public Result<?> updateConfig(@RequestBody WmsPurchaseConfig wmsPurchaseConfig) {
        return wmsPurchaseConfigService.saveOrUpdateConfig(wmsPurchaseConfig);
    }
    
    /**
     * 自动生成采购单
     *
     * @return
     */
    @AutoLog(value = "采购配置-自动生成采购单")
    @ApiOperation(value="采购配置-自动生成采购单", notes="采购配置-自动生成采购单")
    @PostMapping(value = "/generate")
    public Result<?> generatePurchase() {
        // 首先刷新物料预警
        Result<?> refreshResult = wmsPurchaseConfigService.refreshWarnings();
        if (!refreshResult.isSuccess()) {
            return refreshResult;
        }
        
        // 生成采购单
        return wmsPurchaseConfigService.generateAutoPurchaseOrder();
    }
} 